package com.woniuxy.commons.handler;

import com.woniuxy.commons.exception.*;
import com.woniuxy.commons.model.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

/**
 * springmvc提供了全局异常处理
 * 只需要有一个类被@ControllerAdvice注解即可
 * 在该类中会通过不同的方法来完全不同的异常处理，在方法上加@ExceptionHandler注解即可
 * <p>
 * 在全局异常处理中，一般会编写方法来处理不同类型的自定义异常!!!!
 * 如果是java抛出的异常，则会使用兜底操作来完成处理!!!!
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 兜底操作
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        return Result.fail(500, e.getMessage());
    }

    /**
     * 用户相关操作异常统一处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UserException.class)
    public Result UserException(UserException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(LoginException.class)
    public Result LoginException(LoginException e) {
        e.printStackTrace();
        return Result.fail(500, e.getMessage());
    }
    @ExceptionHandler(OrderException.class)
    public Result OrderException(OrderException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(CommentException.class)
    public Result CommentException(CommentException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(AddressException.class)
    public Result AddressException(AddressException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(CartException.class)
    public Result CartException(CartException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(CommentAttentException.class)
    public Result CommentAttentException(CommentAttentException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(OrderFoodException.class)
    public Result OrderFoodException(OrderFoodException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(OrderLogException.class)
    public Result OrderLogException(OrderLogException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(StoreException.class)
    public Result StoreException(StoreException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(ProductException.class)
    public Result ProductException(ProductException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(FoodException.class)
    public Result FoodException(FoodException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }

    //没有权限时访问资源抛出的异常
    @ExceptionHandler(AccessDeniedException.class)
    public Result accessDeniedExceptionHandler(AccessDeniedException e) {
        e.printStackTrace();
        return Result.fail(403, e.getMessage());
    }

    @ExceptionHandler(NumberFormatException.class)
    public Result accessDeniedExceptionHandler(NumberFormatException e) {
        e.printStackTrace();
        return Result.fail(500, "参数格式错误");
    }


}
